

//
//  LYSendCodeButton.m
//  SelectCity
//
//  Created by liuyan on 2019/3/28.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYSendCodeButton.h"

@interface LYSendCodeButton()

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation LYSendCodeButton

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.backgroundColor = [UIColor whiteColor];
    self.titleLabel.font = Font(Font_13);
    [self setTitleColor:THEMECOLOR_OF_TEXT_333333 forState:UIControlStateNormal];
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
}

- (void)timeFailBeginFrom:(NSInteger)timeCount {
    self.count = timeCount;
    self.enabled = NO;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}

- (void)timerFired {
    if (self.count != 1) {
        self.count -= 1;
        NSString *title = [NSString stringWithFormat:@"%ld秒后重发", self.count];
        [self setTitleColor:THEMECOLOR_OF_ORANGE forState:UIControlStateNormal];
        [self setTitle:title forState:UIControlStateNormal];
    } else {
        self.enabled = YES;
        [self setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self setTitleColor:THEMECOLOR_OF_TEXT_333333 forState:UIControlStateNormal];
        [self.timer invalidate];
    }
}

@end
